package com.atguigu.service.impl;

import com.atguigu.dao.ContainerPortMapDao;
import com.atguigu.dao.impl.ContainerPortMapDaoImpl;
import com.atguigu.pojo.ContainerPortMap;
import com.atguigu.pojo.Page;
import com.atguigu.service.ContainerPortMapService;

import java.util.List;

public class ContainerPortMapServiceImpl implements ContainerPortMapService {
    ContainerPortMapDao containerPortMapDao=new ContainerPortMapDaoImpl();
    @Override
    public void addContainerPortMap(ContainerPortMap containerPortMap) {
        containerPortMapDao.addContainerPortMap(containerPortMap);
    }

    @Override
    public void updateContainerPortMap(ContainerPortMap containerPortMap) {
        containerPortMapDao.updateContainerPortMap(containerPortMap);
    }

    @Override
    public void deleteContainerPortMapById(String id) {
        containerPortMapDao.deleteContainerPortMapById(id);
    }
    @Override
    public void deleteContainerPortMapByContainerId(String containerId) {
        containerPortMapDao.deleteContainerPortMapByContainerId(containerId);
    }

    @Override
    public ContainerPortMap queryContainerPortMapById(String id) {
        ContainerPortMap containerPortMap=containerPortMapDao.queryContainerPortMapById(id);
        return containerPortMap;
    }
    @Override
    public ContainerPortMap queryContainerPortMapsByContainerIdAndOut(String containerId,String out) {
        ContainerPortMap containerPortMap=containerPortMapDao.queryContainerPortMapsByContainerIdAndOut(containerId,out);
        return containerPortMap;
    }
    @Override
    public ContainerPortMap queryContainerPortMapsByContainerIdAndIn(String containerId,String in) {
        ContainerPortMap containerPortMap=containerPortMapDao.queryContainerPortMapsByContainerIdAndIn(containerId,in);
        return containerPortMap;
    }

    @Override
    public List<ContainerPortMap> queryContainerPortMaps() {
        List<ContainerPortMap> containerPortMaps=containerPortMapDao.queryContainerPortMaps();
        return containerPortMaps;
    }
    @Override
    public List<ContainerPortMap> queryContainerPortMapByContainerId(String containerId){
        List<ContainerPortMap> containerPortMaps=containerPortMapDao.queryContainerPortMapByContainerId(containerId);
        return containerPortMaps;
    }

    @Override
    public Page page(Integer pageNo, Integer pageSize) {
        Page<ContainerPortMap> page=new Page<>();
        page.setPagesize(pageSize);
        Integer pageTotalCount=containerPortMapDao.queryForPageTotalCount();
        page.setPageTotalCount(pageTotalCount);
        Integer pageTotal=pageTotalCount/pageSize;
        if(pageTotalCount % pageSize >0){
            pageTotal++;//总条数,除以一页条数,大于0,页码数+1
        }
        page.setPageTotal(pageTotal);
        System.out.println("pageNo 原来是 is:"+pageNo);
        if(pageNo<1){//防止用户输入量太大
            pageNo=1;
        }else if(pageNo>pageTotal){
            pageNo=pageTotal;
        }
        page.setPageNo(pageNo);//当前页码
        System.out.println("pageNo 现在是 is:"+pageNo);
        System.out.println("pageTotal is:"+pageTotal);
        //要得到的是page的items
        Integer begin=(page.getPageNo()-1)*pageSize;
        List<ContainerPortMap> items=containerPortMapDao.queryForPageItems(begin,pageSize);
        page.setItems(items);
        return page;
    }
}
